using UnityEngine;
using System.Collections;
using Holoville.HOTween;

public class PinchZoom2 : MonoBehaviour
{
	public float screenHdefault = 600;
	public float screenWdefault = 1024;
	public GameObject goZoom;
	public UISprite pic;

	public float zoomFactor = 1.0f;
	public float timeZoom = 0.3f;

	Tweener tweenerMin = null;
	Tweener tweenerMax = null;
	Tweener tweenerMov = null;
	Tweener tweenerZoo = null;

	enum StatePic : int
	{
		FitHeight = 0,
		FitWidth = 1,
		Default = 2, // 1:1
		CountStateAutoScale = 3,
		Free = 4,
	}

	StatePic currState = StatePic.Default;
	StatePic lastState = StatePic.Default;
	StatePic reseState = StatePic.Default;
	StatePic lockAutoMoveState = StatePic.Default;

	float scaleFitH = 0;
	float scaleFitW = 0;
	float scaleDefault = 1.0f;
	float maxZoom = 1.3f;
	float minZoom = 1;

	int picH = 0;
	int picW = 0;
	public bool abc = true;
	private void RecheckMaxMin()
	{
		if (abc)
		{
			picH = pic.atlas.spriteList[0].height;
			picW = pic.atlas.spriteList[0].width;
		}
		else
		{
			picH = pic.atlas.spriteList[0].width;
			picW = pic.atlas.spriteList[0].height;
		}
		float tt = Screen.height / screenHdefault;
		scaleFitH = screenHdefault / (float)picH;
		scaleFitW = ((float)Screen.width) / tt / (float)picW;
		//if (picH < picW)
		{
			if (scaleFitH < scaleFitW)
			{
				currState = StatePic.FitWidth;
				reseState = StatePic.FitWidth;
				lockAutoMoveState = StatePic.FitHeight;
				minZoom = scaleFitH * 8f / 10f;
			}
			else
			{
				currState = StatePic.FitHeight;
				reseState = StatePic.FitHeight;
				lockAutoMoveState = StatePic.FitWidth;
				minZoom = scaleFitW * 8f / 10f;
			}
			if (scaleFitH  > maxZoom )
			{
				maxZoom = scaleFitH;
			}
			if (scaleFitW > maxZoom)
			{
				maxZoom = scaleFitW;
			}
			if (scaleDefault > maxZoom)
			{
				maxZoom = scaleDefault;
			}
			maxZoom = 13 * maxZoom / 10;
		}
// 		else
// 		{
// 			if (scaleFitH > scaleFitW)
// 			{
// 				currState = StatePic.FitWidth;
// 				reseState = StatePic.FitWidth;
// 				lockAutoMoveState = StatePic.FitWidth;
// 				minZoom = scaleFitW * 80f / 10f;
// 			}
// 			else
// 			{
// 				currState = StatePic.FitHeight;
// 				reseState = StatePic.FitHeight;
// 				lockAutoMoveState = StatePic.FitHeight;
// 				minZoom = scaleFitH * 8f / 10f;
// 			}
// 		}
	}

	public void DownloadPicDone()
	{
		Debug.Log("width " + Screen.width + " height " + Screen.height);
		RecheckMaxMin();
		countClick = (int)currState;
	}

	void Update()
	{
		if (lastState != currState)
		{
			switch (currState)
			{
				case StatePic.Default:
					Debug.Log("StatePic.Default");
					SetZoom(scaleDefault);
					MovePicToPos(Vector3.zero, timeZoom);
					break;
				case StatePic.FitHeight:
					Debug.Log("StatePic.FitHeight");
					SetZoom(scaleFitH);
					MovePicToPos(Vector3.zero, timeZoom);
					break;
				case StatePic.FitWidth:
					Debug.Log("StatePic.FitWidth");
					SetZoom(scaleFitW);
					MovePicToPos(Vector3.zero, timeZoom);
					break;
				case StatePic.Free:
					break;
			}
			lastState = currState;
		}
		else
		{
			if (Input.touchCount == 0)
			{
				if (zoomFactor > maxZoom && (tweenerZoo == null || tweenerZoo.isComplete))
				{
					Debug.Log("zoomFactor > maxZoom " + zoomFactor + " > " + maxZoom);
					tweenerZoo = HOTween.To(this, timeZoom, new TweenParms().Prop("zoomFactor", maxZoom).Ease(EaseType.Linear).OnUpdate(UpdateZoom));	
				}
				if (zoomFactor < minZoom && (tweenerZoo == null || tweenerZoo.isComplete))
				{
					Debug.Log("zoomFactor < minZoom " + zoomFactor + " < " + minZoom);
					tweenerZoo = HOTween.To(this, timeZoom, new TweenParms().Prop("zoomFactor", minZoom).Ease(EaseType.Linear).OnUpdate(UpdateZoom));	
				}
				if (currState != lockAutoMoveState)
				{
					Vector3 cenPic = goZoom.transform.position;
					float tt = Screen.height / screenHdefault;
					Vector3 posTopRightMax =
						camUI.ScreenToWorldPoint(
						new Vector3((float)picW * zoomFactor / 2f*tt, tt*(float)picH * zoomFactor / 2f, 0)
												);

					if (cenPic.x > posTopRightMax.x)
					{
						cenPic.x = posTopRightMax.x;
					}
					if (cenPic.y > posTopRightMax.y)
					{
						cenPic.y = posTopRightMax.y;
					}

					Vector3 posBotLeftMin =
						camUI.ScreenToWorldPoint(
						new Vector3(((float)Screen.width - (float)picW / 2f * zoomFactor*tt), ((float)Screen.height - tt*(float)picH / 2f * zoomFactor), 0)
												);

					if (cenPic.x < posBotLeftMin.x)
					{
						cenPic.x = posBotLeftMin.x;
					}
					if (cenPic.y < posBotLeftMin.y)
					{
						cenPic.y = posBotLeftMin.y;
					}
					goZoom.transform.position = cenPic;
				}
			}
		}
	}

	public GameObject goTopRight;
	public GameObject goBotLeft;
	public Camera camUI;
	void OnGUI()
	{
		if (Input.touchCount >= 2)
		{
			currState = StatePic.Free;
			Vector2 lastTouch1 = Input.GetTouch(0).position;
			Vector2 lastTouch2 = Input.GetTouch(1).position;

			Vector2 currTouch1 = lastTouch1 + Input.GetTouch(0).deltaPosition;
			Vector2 currTouch2 = lastTouch2 + Input.GetTouch(1).deltaPosition;

			Vector2 middleTouch = (currTouch1 + currTouch2) / 2;
			Vector3 middleInWorld = camUI.ScreenToWorldPoint(new Vector3(middleTouch.x, middleTouch.y, 0));

			float lastDist = Vector3.Distance(goZoom.transform.position, middleInWorld);

			float pixelLastCount = Vector2.Distance(lastTouch1, lastTouch2);
			float pixelCurrCount = Vector2.Distance(currTouch1, currTouch2);
			float newScale = pixelCurrCount / pixelLastCount;
			zoomFactor *= newScale;
			UpdateZoom();
			Vector3 addVT = new Vector3(goZoom.transform.position.x - middleInWorld.x, goZoom.transform.position.y - middleInWorld.y, 0);
			goZoom.transform.position = middleInWorld + addVT * newScale;
		}
		else
			if (currState != lockAutoMoveState)
			{
				if (Input.touchCount == 1)
				{
					goZoom.transform.position += (new Vector3(Input.GetTouch(0).deltaPosition.x, Input.GetTouch(0).deltaPosition.y, 0))
						* ((goTopRight.transform.position.y - goBotLeft.transform.position.y) / Screen.height);
				}
				else
					if (Input.GetMouseButton(0))
					{
						if (isGetMouse)
						{
							lastMouse = Input.mousePosition;
							isGetMouse = false;
						}
						goZoom.transform.position += (new Vector3(-(lastMouse - Input.mousePosition).x, -(lastMouse - Input.mousePosition).y, 0))
							* ((goTopRight.transform.position.y - goBotLeft.transform.position.y) / Screen.height);
						lastMouse = Input.mousePosition;
					}
					else
					{
						isGetMouse = true;
					}
			}
	}
	Vector3 lastMouse = Vector3.zero;
	bool isGetMouse = false;
	private void MovePicToPos(Vector3 pos, float time)
	{
		if (tweenerMov != null && tweenerMov.isComplete == false)
		{
			tweenerMov.Complete();
		}
		tweenerMov = HOTween.To(goZoom.transform, time, new TweenParms().Prop("localPosition", pos).Ease(EaseType.Linear));
	}

	private void SetZoom(float zoom)
	{
		NotifyUISystem.instance.ShowNotify(currState.ToString());
		if (tweenerZoo != null && tweenerZoo.isComplete == false)
		{
			tweenerZoo.Complete();
		}
		tweenerZoo = HOTween.To(this, timeZoom, new TweenParms().Prop("zoomFactor", zoom).Ease(EaseType.Linear).OnUpdate(UpdateZoom));	
	}

	Vector3 scaleV = new Vector3();

	void UpdateZoom()
	{
		scaleV.x = zoomFactor;
		scaleV.y = zoomFactor;
		scaleV.z = zoomFactor;
		goZoom.transform.localScale = scaleV;
	}

	int countClick = -1;
	void PicClick()
	{		
		if (Input.touchCount <= 1)
		{
			if (currState == StatePic.Free)
			{
				currState = reseState;
				countClick = (int)currState;
			}
			else
			{
				countClick++;
				currState = (StatePic)(countClick % ((int)StatePic.CountStateAutoScale));
			}
		}
	}
}